package com.dongdongshop.controller;

import com.dongdongshop.common.AjaxResult;
import com.dongdongshop.common.ResultEnum;
import com.dongdongshop.pojo.TbSeller;
import com.dongdongshop.service.TbSellerService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("seller")
public class SellerController {
    Logger logger = LoggerFactory.getLogger(SellerController.class);
    @DubboReference
    private TbSellerService tbSellerService;

    /*验证账号是否存在*/
    @RequestMapping("getSellerById")
    @ResponseBody
    public AjaxResult getSellerById(String sellerId){

        if (sellerId ==null||"".equals(sellerId)){
            return AjaxResult.R(ResultEnum.ACCOUNT_ISNULL);
        }
        try {
            TbSeller seller = tbSellerService.selectBySellerId(sellerId);
            if (seller != null){
                return AjaxResult.R(ResultEnum.ACCOUNT_ALREADY_EXISTS);
            }
            return AjaxResult.R(ResultEnum.SUCCESS);
        }catch (Exception e){
            logger.error("验证账号是否存在!查询错误!错误详情:{}",e);
            return AjaxResult.R(ResultEnum.ACCOUNT_ALREADY_EXISTS);
        }
    }

    /*商家注册*/
    @RequestMapping("getRegisterSeller")
    @ResponseBody
    public AjaxResult getRegisterSeller(TbSeller seller){

        if (seller.getSellerId() ==null||"".equals(seller.getSellerId())){
            return AjaxResult.R(ResultEnum.ACCOUNT_REGISTER_NO);
        }
        try {
            int i = tbSellerService.getRegisterSeller(seller);
            if (i>0){
                return AjaxResult.R(ResultEnum.ACCOUNT_REGISTER_YES);
            }
            return AjaxResult.R(ResultEnum.SUCCESS);
        }catch (Exception e){
            logger.error("账号注册失败!错误详情:{}",e);
            return AjaxResult.R(ResultEnum.ACCOUNT_REGISTER_NO);
        }
    }
    /*商家信息*/
    @RequestMapping("getSeller")
    public String getSelect(Model model) {
        /*获取用户资料*/
        TbSeller seller = (TbSeller) SecurityUtils.getSubject().getPrincipal();
        model.addAttribute("sel",seller);
        return "/admin/seller";
    }
    /*修改商家信息*/
    @RequestMapping("getUpdateSeller")
    @ResponseBody
    public AjaxResult getUpdateSeller(TbSeller seller) {
        try {
            int i = tbSellerService.getUpdateSeller(seller);
            if (i>0){
                return AjaxResult.R(ResultEnum.BUSINESS_UPDATE_YES);
            }
            return AjaxResult.R(ResultEnum.SUCCESS);
        }catch (Exception e){
            logger.error("修改商家信息失败!错误详情:{}",e);
            return AjaxResult.R(ResultEnum.BUSINESS_UPDATE_NO);
        }
    }
}
